<?php

class Adsent_modul  extends CI_Model {
	public $select;

	# id телефона
	public $id_phone;

	public function __construct() {
		$this -> load -> library('session');
		$this -> id_phone = $this -> session -> userdata('id_phone');
		// Call the Model constructor
		parent::__construct();
	}

	public function region($id = false) {
		$time = false;

		$DB2 = $this -> load -> database('adsent', TRUE, TRUE);

		if ($id == false) {

			$select = "SELECT name, code,fullname,region,  id FROM region  WHERE (code LIKE '__00000000000') ORDER BY sort ASC ;";
		} else {

			$select = "SELECT name, code,fullname,region,  id FROM region WHERE (code LIKE '__00000000000') and `id`='$id' ORDER BY sort ASC;";
		}
		$q = $DB2 -> query($select);

		if ($q -> num_rows() > 0) {

			if ($id == false) {
				foreach ($q -> result() as $row) {

					$time[] = array(
					'id' => $row -> id,
					 'name' => $row -> name, 
					 'region' => $row -> region, 
					 'fullname' => $row -> fullname, 
					 'code' => $row -> code,
					  );
				}
			} else {
				$select = "SELECT *  from region where  `id` = $id ORDER BY id ASC;";
				$row = $q -> row();
				$time['id'] = $row -> id;
				$time['code'] = $row -> code;
				$time['name'] = $row -> name;
				$time['fullname'] = $row -> fullname;
				$time['region'] = $row -> region;

			}
		}
		return $time;
	}

	public function metro($city = 1) {
		$time = false; 
		
		$DB2 = $this -> load -> database('adsent', TRUE, TRUE);
		$select = "SELECT * FROM `metro` where `city` = '$city' ;";

		$q = $DB2 -> query($select);

		if ($q -> num_rows() > 0) {

			foreach ($q -> result() as $row) {

				$m[] = array('id' => $row -> id, 'name' => $row -> name, 'city' => $row -> city, );
			}
		}
		return $m;
	}

	/**
	 * Поиск метро при поиске квартиры
	 *
	 * @param  char $street = название улицы
	 * @author
	 *
	 *
	 */
	public function metrosearch($city = 1) {
		$time = false;

		$DB2 = $this -> load -> database('adsent', TRUE, TRUE);

		$select = "SELECT * FROM `metro` where city = $city ;";

		$q = $DB2 -> query($select);

		if ($q -> num_rows() > 0) {

			foreach ($q -> result() as $row) {

				$m[] = array('id' => $row -> id, 'name' => $row -> name, 'xline' => $row -> xline, 'yline' => $row -> yline, 'city' => $row -> city, );
			}
		}
		return $m;
	}

	/**
	 * Поиск города/села в регионе
	 *
	 * @param  char $street = название улицы
	 * @param  int $code = код города или населённого пункта
	 * @return array = данные о улице
	 * @author
	 *
	 *
	 */

	public function street($street, $code) {
		$this -> load -> helper('text');
		$time = array();
		// поиск улицы в регионе или городе
		$DB2 = $this -> load -> database('adsent', TRUE, TRUE);

		$select = " SELECT street.* 
			        FROM street join sh_street AS sh ON street.id = sh.id  
			        WHERE   
			        MATCH(sh.name) AGAINST ('" . $street . "*' IN BOOLEAN MODE) 
			        AND
			          MATCH(sh.code) AGAINST ('" . substr($code, 0, 11) . "*' IN BOOLEAN MODE);";
		$q = $DB2 -> query($select);

		if ($q -> num_rows() > 0) {

			foreach ($q -> result() as $row) {

				$socr = search_street($row -> socr);
				$time[] = array('name' => $row -> name, 'code' => $row -> code, 'property' => $socr, );
			}
		} else {
			return 0;
		}
		return $time;
	}

	# 7700000000000
	#   926046  Бирюлевская ул  77000000000047000       7724        45296553000 770000000000%

	/**
	 * Поиск города/села в регионе
	 *
	 * @param  char $street = название улицы
	 * @param  int $code = код города или населённого пункта
	 * @return array = данные о улице
	 * @author
	 */

	public function capital($street, $code) {
		$this -> load -> helper('text');
		$time = array();
		// поиск улицы в регионе или городе
		$DB2 = $this -> load -> database('adsent', TRUE, TRUE);

		$select = " SELECT street.* 
        FROM street join `capital_street` c ON street.id = c.id  
        WHERE   
        MATCH(c.name) AGAINST ('" . $street . "*' IN BOOLEAN MODE) 
        AND
          MATCH(c.code) AGAINST ('" . substr($code, 0, 11) . "*' IN BOOLEAN MODE);";
		$q = $DB2 -> query($select);

		if ($q -> num_rows() > 0) {

			foreach ($q -> result() as $row) {
				$socr = search_street($row -> socr);
				$time[] = array('name' => $row -> name, 'code' => $row -> code, 'property' => $socr, );
			}

		} else {

			return 0;
		}
		return $time;
	}

	/**
	 * Поиск города/села в регионе
	 *
	 * @param  char $city = название города
	 * @param  int $code = код региона
	 * @return array = данные о улице
	 * @author
	 */

	public function city($city, $code) {
		$time = array();
		$this -> load -> helper('text');
		// поиск улицы в регионе или городе

		// " . substr($code, 0, 2) . "%'  "'.substr($code, 0, 2).'"
		$DB2 = $this -> load -> database('adsent', TRUE, TRUE);

		$len = substr($code, 0, 2);

		# return;

		# АО Аобл  край обл Респ р-н тер у

		$select = 'SELECT 

(SELECT  name   FROM `sh_kladr` sh where sh.code  = CONCAT( LEFT( kladr.code, 5 ), "00000000") limit 1 ) AS areaname,

(SELECT  socr   FROM `sh_kladr` sh where sh.code  = CONCAT( LEFT( kladr.code, 5 ), "00000000") limit 1 ) AS areasocr
, kladr.*   
					    FROM `kladr` 
					    INNER JOIN `sh_kladr` 
					    ON `kladr`.`id` = `sh_kladr`.`id` 
					    WHERE 
					MATCH(`sh_kladr`.name) AGAINST ("' . $city . '*" IN BOOLEAN MODE)
					    AND (`sh_kladr`.code LIKE "' . substr($code, 0, 2) . '___00000000" 
					OR `sh_kladr`.code LIKE "' . substr($code, 0, 2) . '000___00000" 
					OR `sh_kladr`.code LIKE "' . substr($code, 0, 2) . '000000___00" 
					OR `sh_kladr`.code LIKE "' . substr($code, 0, 2) . '___000___00")
					AND (sh_kladr.socr != "АО" AND 
					sh_kladr.socr != "Аобл" AND 
					sh_kladr.socr != "край" AND 
					sh_kladr.socr != "обл" AND 
					sh_kladr.socr != "Респ" AND 
					sh_kladr.socr != "р-н" AND 
					sh_kladr.socr != "тер");';

		$q = $DB2 -> query($select);

		if ($q -> num_rows() > 0) {

			foreach ($q -> result() as $row) {

				$socr = search_city($row -> socr);
				$areasocr = search_city($row -> areasocr);

				$time[] = array('areaname' => $row -> areaname, 'areasocr' => $areasocr, 'name' => $row -> name, 'code' => $row -> code, 'property' => $socr, 'gninmb' => $row -> gninmb, );
			}

		}
		if (!isset($time['0'])) {
			return 0;
		}

		return $time;
	}

	public function capital_home($home, $code) {

		$time = array();
		$long = '';
		// поиск дома на  улице в регионе или городе

		$DB2 = $this -> load -> database('adsent', TRUE, TRUE);

		$select = 'SELECT doma.* 
					FROM capital_doma join doma on capital_doma.id = doma.id  
					WHERE (MATCH(capital_doma.code) AGAINST ("' . $code . '*"  IN BOOLEAN MODE) ) 
					 and  capital_doma.name like  "%' . $home . '%";';

		$q = $DB2 -> query($select);

		if ($q -> num_rows() > 0) {

			foreach ($q -> result() as $row) {

				$long .= $row -> name . ' , ';

				$time[] = array('name' => $row -> name, 'code' => $row -> code, 'socr' => $row -> socr, 'korp' => $row -> korp, 'index' => $row -> index, 'gninmb' => $row -> gninmb, 'uno' => $row -> uno, 'ocatd' => $row -> ocatd, );
			}

		} else {

			$select2 = 'SELECT doma.* 
					FROM capital_doma join doma on capital_doma.id = doma.id  
					WHERE (MATCH(capital_doma.code) AGAINST ("' . $code . '*"  IN BOOLEAN MODE) );';
			$q2 = $DB2 -> query($select2);

			if ($q2 -> num_rows() > 0) {
				foreach ($q2 -> result() as $row2) {

					$long .= $row2 -> name . ' , ';

					$time[] = array('name' => $row2 -> name, 'code' => $row2 -> code, 'socr' => $row2 -> socr, 'korp' => $row2 -> korp, 'index' => $row2 -> index, 'gninmb' => $row2 -> gninmb, 'uno' => $row2 -> uno, 'ocatd' => $row2 -> ocatd, );
				}

			} else {
				return 0;

			}
		}

		$a['home'] = sort($time);
		$a['long'] = $long;

		return $a;
	}

	public function home($home, $code) {
		$time = array();
		$long = '';
		// поиск дома на  улице в регионе или городе

		$DB2 = $this -> load -> database('adsent', TRUE, TRUE);

		$select = 'SELECT doma.* 
                    FROM sh_doma join doma on sh_doma.id = doma.id  
                    WHERE  
                    (MATCH(sh_doma.code) AGAINST ("' . $code . '*"  IN BOOLEAN MODE) )
                    and  sh_doma.name like  "%' . $home . '%";';

		$q = $DB2 -> query($select);
		if ($q -> num_rows() > 0) {
			foreach ($q -> result() as $row) {
				$long .= $row -> name . ' , ';
				$time[] = array('name' => $row -> name, 'code' => $row -> code, 'socr' => $row -> socr, 'korp' => $row -> korp, 'index' => $row -> index, 'gninmb' => $row -> gninmb, 'uno' => $row -> uno, 'ocatd' => $row -> ocatd, );
			}

		} else {

			$select2 = 'SELECT doma.* 
                    FROM sh_doma join doma on sh_doma.id = doma.id  
                    WHERE  
                    MATCH(sh_doma.code) AGAINST ("' . $code . '*"  IN BOOLEAN MODE) ;';

			$q2 = $DB2 -> query($select2);
			if ($q2 -> num_rows() > 0) {
				foreach ($q2 -> result() as $row2) {
					$long .= $row2 -> name . ' , ';
					$time[] = array('name' => $row2 -> name, 'code' => $row2 -> code, 'socr' => $row2 -> socr, 'korp' => $row2 -> korp, 'index' => $row2 -> index, 'gninmb' => $row2 -> gninmb, 'uno' => $row2 -> uno, 'ocatd' => $row2 -> ocatd, );
				}

			} else {

				return 0;
			}

		}

		$a['home'] = $time;
		$a['long'] = $long;

		return $a;
	}

	public function foto($adsent = '1', $file = '1',$type='bad') {

		$DB2 = $this -> load -> database('adsent', TRUE, TRUE);
		$data['datetime'] = date("Y-m-d H:i:s", time());
		$data['name'] = $file;
        $data['adsent'] = $adsent;
        $data['type'] = $type;
		$data['id_phone'] = $this -> id_phone;
		$DB2 -> insert('img', $data);
		$id = $DB2 -> insert_id();
		return $id;
	}








/**
 * undocumented function
 *
 * @return void
 * @author  
 */
function rq($output) {
	if(isset($output['region'])){ 
		if($output['region'] < 9900000000000 && $output['region'] > 100000000000 ){
			return $output;
		}else{
			
				return  $output['region'] = 7700000000000;
		}
		
			return;		
	}else{
		return  $output['region'] = 7700000000000;
		 
	}
	
}

 


}

# http://www.ksoft.ru/opis_kladr.htm#_Toc137607893
